package acm.usa2006.gnewyork;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/*
 * p2539
 * Greater New York 2006
 */

public class p2539 {

	static int nTest, N, step;
	static String s1, s2, s, ns;
	static StringBuilder builder;
	static Set<String> set;

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		nTest = scanner.nextInt();
		
		for (int i=1;i<=nTest;++i) {
//			System.out.print(N + " ");
//			scanner.nextLine();
			set = new HashSet<String>();
			N = scanner.nextInt();
			s1 = scanner.next();
			s2 = scanner.next();
			s = scanner.next();
			set.add(s);
			builder = new StringBuilder();
			for (int j=0;j<N;++j) {
				builder.append(s2.charAt(j));
				builder.append(s1.charAt(j));
			}
			ns = builder.toString();
			step = 1;
			while (true) {
				if (ns.compareTo(s) == 0) {
					System.out.println(i + " " + step);
					break;
				}
				else if (set.contains(ns)) {
					step = -1;
					System.out.println(i + " " + step);
					break;
				}
				else {
					set.add(ns);
					++step;
					s1 = ns.substring(0, N);
					s2 = ns.substring(N);
					builder = new StringBuilder();
					for (int j=0;j<N;++j) {
						builder.append(s2.charAt(j));
						builder.append(s1.charAt(j));
					}
					ns = builder.toString();
				}
			}
//			if (step > 6) {
//				step = -1;
//				System.out.println(i + " " + step);
//			}
		}

	}

}
